import unittest 
from pygo1963.model.Board import Board
from pygo1963.model.Constants import WHITE_COLOR, BLACK_COLOR
from pygo1963.model.Vertex import Vertex
from pygo1963.model.Move import Move

class GroupTest(unittest.TestCase):
    """
    Tests the groups functionality
    """
    
    def setUp(self):
        self.board = Board(9)
        
    def test_update_liberties(self):

        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b2")))
        group = self.board.get_group(Vertex.from_string("b2"))

        self.assertEqual(len(group.liberties), 4)
        self.assertTrue(Vertex.from_string("b1") in group.liberties)
        self.assertTrue(Vertex.from_string("b3") in group.liberties)
        self.assertTrue(Vertex.from_string("a2") in group.liberties)
        self.assertTrue(Vertex.from_string("c2") in group.liberties)
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b1")))

        self.assertEqual(len(group.liberties), 5)
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("d1")))
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("c1")))
        
        group = self.board.get_group(Vertex.from_string("b2"))

        self.assertEqual(len(group.liberties), 6)
        
    def test_is_captured_corner(self):

        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1")))
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("b1")))
        
        group = self.board.get_group(Vertex.from_string("a1"))
        self.assertEqual(len(group.liberties), 1)
        
        capture_move = Move(BLACK_COLOR, Vertex.from_string("a2"))
        #self.assertTrue(group.gets_captured(capture_move))
        
        self.board.make_move(capture_move)
        self.assertTrue(group.is_captured())
        
    def test_is_captured_middle(self):

        white_stone = Vertex.from_string("c3")        
        self.board.make_move(Move(WHITE_COLOR, white_stone))
        
        # Capture C3
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c4")))
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c2")))
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("b3")))
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("d3")))
        
        # print self.board
        
        self.assertTrue(self.board.get_group(white_stone).is_captured())
        self.assertEqual(self.board.winner, BLACK_COLOR)
        
    def test_is_captured_group_simple(self):

        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("c4")))
        
        black_group = self.board.get_group(Vertex.from_string("c4"))
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b4")))
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("d4")))
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("d3")))        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("c3")))
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("c5")))
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("d5")))
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("e5")))
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("e4")))
        
        self.assertTrue(black_group.is_captured())
        
#        print self.board
#        
#        for group in self.board.groups:
#            print group         
        
if __name__ == "__main__":
    unittest.main() 